package com.mingle.iwalk.uploader;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//import javax.naming.PartialResultException;


import org.apache.http.HeaderElement;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
/*import org.apache.http.entity.mime.Header;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;*/
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
//import org.omg.CORBA.ORB;



public class HttpSend {
	
	
	private String userid;
	private String username;
	private String userpass;
	private String basic;
	
	/**
	 * @return the userid
	 */
	public String getUserid() {
		return userid;
	}

	/**
	 * @param userid the userid to set
	 */
	public void setUserid(String userid) {
		this.userid = userid;
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the userpass
	 */
	public String getUserpass() {
		return userpass;
	}

	/**
	 * @param userpass the userpass to set
	 */
	public void setUserpass(String userpass) {
		this.userpass = userpass;
		encodeBasicAuthenticationString();
	}
	
	private void encodeBasicAuthenticationString() {
        if (null != userid && null != this.userpass) {
            this.basic = "Basic " +
                    new String(new BASE64Encoder().encode((userid + ":" + userpass).getBytes()));
           
        }
    }

	public HttpResponse postDataWithFile(String url, PostParameter[] postParameters, File file, boolean authenticated){
	
		//PostParameter[] newPostParameters = Array.copyOf(postParameters,
		//		postParameters.length);
		
		PostParameter[] newPostParameters = new PostParameter[postParameters.length];
		System.arraycopy(postParameters, 0, newPostParameters, 0, postParameters.length);
		
		//maybe we can use this to identify some useID
		//newPostParameters[ postParameters.length]=new PostParameter("source", "idiot/00032");

		return HttpRequestWithFile(url, newPostParameters, file, authenticated);
	}

	public HttpResponse postData(String url, PostParameter[] postParameters,
			boolean authenticated) {

		//PostParameter[] newPostParameters = Arrays.copyOf(postParameters,
		//		postParameters.length);
		
		PostParameter[] newPostParameters = new PostParameter[postParameters.length];
		System.arraycopy(postParameters, 0, newPostParameters, 0, postParameters.length);
		
		//maybe we can use this to identify some useID
		//newPostParameters[ postParameters.length]=new PostParameter("source", "idiot/00032");

		return httpRequest(url, newPostParameters, authenticated);
	}
	
	public HttpResponse HttpRequestWithFile(String url, PostParameter[] postParameters,File file,boolean authenticated){
	
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		MultipartEntity entity = new MultipartEntity();
		
		for(PostParameter postParameter : postParameters)
		{
			try {
				entity.addPart(postParameter.getName(),new StringBody(postParameter.getValue()));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		entity.addPart("file", new FileBody(file));
		
		httppost.setEntity(entity);
		
		if(authenticated){//need authenticated
			
			List<BasicHeader> headers =  new ArrayList<BasicHeader>();
			
			headers.add(new BasicHeader("Authorization", this.basic));
	
			httpclient.getParams().setParameter("http.default-headers", headers);
					
		}
		HttpResponse response;
		try {
			response = httpclient.execute(httppost);
			return response;
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		

	}
	public HttpResponse httpRequest(String url, PostParameter[] postParameters,boolean authenticated) {

		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(url);
		// "http://www.yoursite.com/script.php");
		
		
		try {
			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(postParameters.length);
			for(PostParameter postParameter : postParameters){
				nameValuePairs.add(new BasicNameValuePair(postParameter.getName(),postParameter.getValue()));
			}
			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			
			
			if(authenticated){//need authenticated
								
				List<BasicHeader> headers =  new ArrayList<BasicHeader>();
				
				headers.add(new BasicHeader("Authorization", this.basic));
		
				httpclient.getParams().setParameter("http.default-headers", headers);
						
			}


			// Execute HTTP Post Request
			HttpResponse response = httpclient.execute(httppost);
			return response;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}
